package se.webbzon.boltzmann.minigame.page;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;

import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.font.HorizontalAlignment;
import se.webbzon.oschi01.font.VerticalAlignment;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.textureset.Texture3D;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;

public class PageImage implements PageItem {
	
	// The layout of this page item
	private final PageLayout layout;
	
	// The image to display
	private final ResourceSet<?> image;
	
	// The width and height of the image
	private int width, height;
	
	// The caption of the image
	private String caption;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public PageImage(PageLayout layout, String name) {
		this.layout = layout;
		ResourceSet<?> image = Resources.getImageSet(name);
		if (image == null)
			image = Resources.getTextureSet(name);
		this.image = image;
		width = layout.getImageWidth();
		height = layout.getImageHeight();
		caption = null;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the width of the image. **/
	public int getImageWidth() {
		return width;
	}

	/** Sets the width of the image. **/
	public void setImageWidth(int width) {
		this.width = width;
	}

	/** Returns the image caption of the image. **/
	public String getCaption() {
		return caption;
	}
	
	/** Sets the image caption of the image. **/
	public void setCaption(String caption) {
		this.caption = caption;
	}

	/** Returns the height of the image. **/
	public int getImageHeight() {
		return height;
	}
	
	/** Sets the height of the image. **/
	public void setImageHeight(int height) {
		this.height = height;
	}

	@Override public void draw(WorldCameraPane hud, int x, int y) {
		final Dimension d = new Dimension();
		hud.getLayer().getDimension(d);
		Object o = image.get(0);
		int centerX = x + d.width/2;
		if (o instanceof Image) {
			hud.drawImage((Image) o, centerX - width/2, y, width, height);
		} else if (o instanceof Texture3D) {
			hud.drawTexture((Texture3D) o, centerX - width/2, y, width, height);
		}
		
		if (caption != null && !caption.isEmpty()) {
			hud.setColor(Color.black);
			hud.setFont(layout.getFont(0, false, true));
			hud.setVAlignment(VerticalAlignment.TOP);
			hud.setHAlignment(HorizontalAlignment.CENTER);
			hud.drawString(caption, centerX, y + height + layout.getLineSpacing());
		}
	}

	@Override public int getHeight() {
		int h = height;
		if (caption != null && !caption.isEmpty()) {
			h += layout.getLineSpacing() + layout.getFont(0, false, true).getSize();
		}
		return h;
	}
	
	@Override public void onStep(Page page) {};

}
